package com.toprs.base.config

import com.fasterxml.jackson.databind.ObjectMapper
import com.github.pagehelper.PageHelper
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component


@Order(5)
@Aspect
@Component
class PageAspect {

    @Before("@annotation(com.toprs.base.annotation.PageQuery)")
    fun executePage(joinPoint: JoinPoint) {
        val objectMapper = ObjectMapper()
        val args = objectMapper.readValue<Map<String, Any>>(
            objectMapper.writeValueAsString(
                joinPoint.args[0]
            ), objectMapper.typeFactory.constructParametricType(
                MutableMap::class.java,
                String::class.java,
                Any::class.java
            )
        )
        PageHelper.startPage<Any>(args["pageNo"] as Int, args["pageSize"] as Int)
    }

}